package top.hidoctor.auscultation.base;

import android.app.Activity;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import com.tang.base.BasePresenter;
import com.tang.base.BaseView;
import com.tang.user.UserInfoManager;
import com.tang.util.DialogUtil;
import com.tang.util.StringUtil;
import com.tang.view.LoadingDialog;

import top.hidoctor.auscultation.R;


public class BaseFragment extends Fragment implements BaseView {

    protected Context mContext;
    protected boolean isOnRefresh = false;
    protected boolean isLoadMore = false;
    protected BasePresenter basePresenter;
    private Toast mToast;
    private LoadingDialog dialog;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mContext = activity;
    }


    @Override
    public void onDestroyView() {
        hideDialog();
        if (basePresenter != null) {
            basePresenter.clear();
        }
        super.onDestroyView();
    }


    protected boolean isLogin() {
        String token = UserInfoManager.getInstance().getToken();
        if (StringUtil.isEmpty(token)) {
//            Toast.makeText(mContext, "请登录!", Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }

    protected void showProgressDialog() {
        showProgressDialog("加载中");
    }

    protected void showProgressDialog(String message) {
        showProgressDialog(message, true);
    }

    protected void showProgressDialog(String message, boolean cancelable) {
        if (dialog == null) {
            dialog = new LoadingDialog(mContext, message, R.mipmap.ic_loading);
        }
        dialog.setCancelable(false);
        if (dialog.isShowing()) {
            hideDialog();
            return;
        } else {
            dialog.show();
        }
    }

    protected void hideDialog() {
        if (dialog != null) {
            dialog.dismiss();
            dialog = null;
        }
    }


    /**
     * 带取消和确认的对话框
     *
     * @param title
     * @param message
     * @param listener
     */
    protected void showAlertDialog(String title, String message
            , DialogUtil.DialogButtonClickListener listener) {
        showAlertDialog(title, message, "确认", "取消", true, listener);
    }

    /**
     * 对话框,不需要的参数传null
     *
     * @param title
     * @param message
     * @param positive
     * @param negative
     * @param cancelable
     * @param listener
     */
    protected void showAlertDialog(String title, String message, String positive, String negative
            , boolean cancelable, DialogUtil.DialogButtonClickListener listener) {

        AlertDialog alertDialog = DialogUtil.createAlertDialog(mContext, title, message, positive, negative, cancelable, listener);
        alertDialog.show();
    }

    @Override
    public void showLoading() {
        showProgressDialog();
    }

    @Override
    public void hideLoading() {
        hideDialog();
    }

    @Override
    public void onError(String code, String msg) {
        isOnRefresh = false;
        isLoadMore = false;
        if (msg.equals("用户授权失败")) {
            return;
        }
        showToast(msg);
        hideDialog();
    }

    protected void showToast(String msg) {
        if (msg.equals("用户授权失败")) {
            return;
        }
        if (mToast == null) {
            mToast = Toast.makeText(mContext, msg, Toast.LENGTH_SHORT);
        } else {
            mToast.setText(msg);
            mToast.setDuration(Toast.LENGTH_SHORT);
        }
        mToast.show();
    }


    public void hideSoft(View view) {
        InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
}

